use std::{
    io::{prelude::*, BufReader},
    net::{TcpListener, TcpStream},
};
use std::time::{SystemTime, UNIX_EPOCH};
// --snip--

fn main() {
    let listener = TcpListener::bind("0.0.0.0:8000").unwrap();

    for stream in listener.incoming() {
        let stream = stream.unwrap();
        handle_connection(stream);
    }
}

fn handle_connection(mut stream: TcpStream) {
    // --snip--
    let buf_reader = BufReader::new(&mut stream);
    let option = buf_reader.lines().next();
    if option.is_some() {
        let result = option.unwrap();
        if result.is_ok() {
            let request_url = result.unwrap();
            let contents = match &request_url[..] {
                "GET / HTTP/1.1" => String::from("hello"),
                "POST /Notice/get_notices HTTP/1.1" | "POST /Ads/get_ads HTTP/1.1" => String::from("[]"),
                "POST /FakeLocation/version/checkApkUpdate HTTP/1.1" => String::from("{}"),
                "POST /FakeLocation/app/getAppConfigs HTTP/1.1" => get_app_config(),
                "POST /FakeLocation/goods/getRenewalGoodsList HTTP/1.1" => get_ren_good(),
                "POST /FakeLocation/user/checkUserExist HTTP/1.1" => user_exist(),
                "POST /FakeLocation/user/checkPwdExist HTTP/1.1" => user_exist(),
                "POST /FakeLocation/user/get HTTP/1.1" => get_user(),
                "POST /FakeLocation/user/login HTTP/1.1" => login(),
                _ => String::from("{}"),
            };

            // let length = contents.len();
            let response = format!("HTTP/1.1 200 OK\r\ncontent-type: application/json\r\n\r\n{contents}");
            stream.write_all(response.as_bytes()).unwrap();
        }
    }

}

/// 不能修改 loginName, key, token, userId
fn get_user() -> String{
    println!("/FakeLocation/user/get");
    let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis().to_string();
    let mut bind = String::from("{\"body\":{\"regtime\":1684723232452,\"proindate\":4102358400000,\"createTime\":1684723232451,\"loginType\":\"email\",\"loginName\":\"BobHCrack\",\"updateTime\":0,\"type\":1,\"key\":\"UsrTjEVxwRaQsvV5hD8I3Db8zrkjuavD/O8hJcVLUOkDimJc6ShgOQEzV5srpEPcOL63J2chFQA=\",\"token\":\"T_CrackedByBobH233\"},\"code\":200,\"returnTime\":");
    bind.push_str(time.as_str());
    bind.push_str(",\"success\":true}");
    bind
}

fn login() -> String {
    println!("/FakeLocation/user/login");
    let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis().to_string();
    let mut bind = String::from("{\"body\":{\"regtime\":1684723232452,\"proindate\":4102358400000,\"createTime\":1684723232451,\"loginType\":\"email\",\"loginName\":\"BobHCrack\",\"updateTime\":0,\"type\":1,\"userId\":\"BobH233LoveLerist\",\"key\":\"UsrTjEVxwRaQsvV5hD8I3Db8zrkjuavD/O8hJcVLUOkDimJc6ShgOQEzV5srpEPcOL63J2chFQA=\",\"token\":\"T_CrackedByBobH233\"},\"code\":200,\"returnTime\":");
    bind.push_str(time.as_str());
    bind.push_str(",\"success\":true}");
    bind
}


fn user_exist() -> String {
    println!("/FakeLocation/user/exist");
    let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis().to_string();
    let mut bind = String::from("{\"body\":true,\"code\":200,\"returnTime\":");
    bind.push_str(time.as_str());
    bind.push_str(",\"success\":true}");
    bind
}

fn get_ren_good() -> String {
    println!("/FakeLocation/goods/getRenewalGoodsList");
    let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis().to_string();
    let mut bind = String::from("{\"body\":[{\"createTime\":0,\"description\":\"\",\"id\":\"0011\",\"isAvailable\":1,\"locale\":\"*\",\"name\":\"永久使用无限制\",\"price\":0,\"priceUnit\":\"¥\",\"recommend\":\"BobH\",\"updateTime\":0,\"value\":30,\"weight\":4}],\"code\":200,\"returnTime\":");
    bind.push_str(time.as_str());
    bind.push_str(",\"success\":true}");
    bind
}

fn get_app_config() -> String {
    println!("/FakeLocation/app/getAppConfigs");
    let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis().to_string();
    let mut bind = String::from("{\"body\":{\"createTime\":");
    bind.push_str(time.as_str());
    bind.push_str(",\"disabledApps\":[],\"disabledFuncs\":[],\"disabledInfos\":[],\"isAllowRun\":1,\"isAvailable\":1,\"notice\":\"\",\"updateTime\":0},\"code\":200,\"returnTime\":");
    bind.push_str(time.as_str());
    bind.push_str(",\"success\":true}");
    bind
}



